// pages/appointment/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    beforeData: {},
    // 弹窗是否可见
    dialogVisitable: false,
    // 列表弹窗是否可见
    listDialogViaible: false,
    dialogPlaceholder:'',
    // 弹窗key
    dialogKey: '',
    // 弹窗value
    dialogValue: '',
    // 弹窗key
    listDialogKey: '',
    // 弹窗value
    listDialogValue: '',
    // 弹窗数据
    listDialogData: [],
    // 年级数据
    gradeData: [],
    // 班级数据
    classData: ['未分班'],
    errorMsg: '',
    // 以下是要提交的数据
    // 学号
    studentNumber: '',
    // 学生姓名
    studentName: '',
    // 联系方式
    phoneNumber: '',
    // 学生性别
    sex: '男',
    // 年级
    grade: '',
    // 班级
    classNumber: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    for (let i = 1; i <= 31; i++) {
      this.data.classData.push(i + '班');
    }
    const eventChannel = this.getOpenerEventChannel()
    if (Object.keys(eventChannel).length > 0)
      eventChannel.on('sendData', (data) => {
        this.setData({
          beforeData: data,
          gradeData: data.schoolInfo.gradeList,
          phoneNumber: data.phoneNumber
        });
      })
  },

  onDialogClose() {
    this.setData({
      dialogVisitable: false,
      dialogValue: ''
    })
  },
  onDialogSubmit(e) {
    let data = {
      dialogVisitable: false,
      dialogValue: ''
    }
    data[e.detail.key] = e.detail.value;
    this.setData(data)
  },
  handelInputClick(event) {
    this.setData({
      dialogKey: event.currentTarget.dataset.key,
      dialogValue: this.data[event.currentTarget.dataset.key],
      dialogVisitable: true,
      dialogPlaceholder:event.currentTarget.dataset.placeholder
    })
  },
  handelSelectClick(event) {
    let key = event.currentTarget.dataset.key;
    this.setData({
      listDialogKey: key,
      listDialogViaible: true,
      listDialogData: key == 'grade' ? this.data.gradeData : this.data.classData,
      listDialogValue: this.data[key],
    })
  },
  onListDialogClose() {
    this.setData({
      listDialogViaible: false,
      listDialogValue: ''
    })
  },
  onListDialogSubmit(e) {
    let data = {
      listDialogViaible: false,
      listDialogValue: ''
    }
    data[e.detail.key] = e.detail.value;
    this.setData(data)
  },
  handelNextPageBtnClick() {
    let msg = '';
    if (!this.data.studentNumber) {
      msg = '输入您的学号或准考证号';
    } else if (!this.data.studentName) {
      msg = '输入您的学生真实姓名';
    } else if (!this.data.phoneNumber) {
      msg = '输入您的联系方式';
    } else if (!this.data.grade) {
      msg = '请选择您的学生年级';
    } else if (!this.data.classNumber) {
      msg = '请选择您的学生班级';
    }
    if (msg) {
      this.setData({
        errorMsg: msg
      });
      return;
    }
    wx.navigateTo({
      url: "../selectSize/index",
      success: (res) => {
        res.eventChannel.emit('sendData', {
          ...this.data.beforeData,
          studentNumber: this.data.studentNumber,
          studentName: this.data.studentName,
          grade: this.data.grade,
          classNumber: this.data.classNumber,
          sex: this.data.sex
        })
      }
    })
  },
  handelSexBtnClick(event) {
    this.setData({
      sex: event.currentTarget.dataset.key
    })
  },
  closeMsgDialog() {
    this.setData({
      errorMsg: ''
    });
  }
})